/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlayerChromosomeComponent.java
 *
 * Created on 9/Dez/2011, 14:19:19
 */

package intopo.evogame.partnerselection.tools.config;

import intopo.evogame.partnerselection.config.TextConfigFilePlainPlayersEditor.Chromosome;

import java.awt.Component;
import java.util.EventObject;
import javax.swing.BorderFactory;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.EventListenerList;

/**
 * The component that displays the chromosome parameters of a player in the configuration file.  This component is used as the renderer and the editor for the table that displays the players in a node.
 * The user can edit the quantity, the chromosome and the strategy.  The chromosome is optionaly as default genes can be used for the chromosome.
 *
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 9/Dez/2011
 */
public class PlayerChromosomeComponent
	extends javax.swing.JPanel
{
	Chromosome chromosome;

//	private EventListenerList eventListener;
	/**
	 * Indicates if any of the text fields as an invalid value.
	 */
	private int error;
	/**
	 * Creates new form PlayerChromosomeComponent
	 */
	public PlayerChromosomeComponent ()
	{
		chromosome = new Chromosome ();
		initComponents ();
//		this.eventListener = new EventListenerList ();
		error = 0;
	}
	/**
	 * Get the chromosome that is being edited in this component.  If there is an error, returns {@code null}.
	 * @return
	 */
	public Chromosome getChromosome ()
	{
		return (this.error == 0 ? this.chromosome :  null);
	}
	/**
	 * Changes the chromosome that is displayed by this component.
	 *
	 * @param chromosome
	 */
	void setChromosome (Chromosome chromosome)
	{
		this.chromosome = chromosome;
		this.error = 0;
		this.poolSizeFormattedTextField.setValue (new Integer (chromosome.getPoolSize ()));
		this.utilityThresholdFormattedTextField.setValue (new Float (chromosome.getUtilityThreshold ()));
		this.deltaFormattedTextField.setValue (new Float (chromosome.getDelta ()));
		this.bitsPerProbabilityFormattedTextField.setValue (new Byte (chromosome.getBitsPerProbability ()));
	}

	void setLabelsVisible (boolean value)
	{
		this.labelsPanel.setVisible (value);
	}

//	protected void fireCellChanged ()
//	{
//
//		// Guaranteed to return a non-null array
//		Object[] listeners = this.listenerList.getListenerList ();
//		// Process the listeners last to first, notifying
//		// those that are interested in this event
//		for (int i = listeners.length-2; i>=0; i-=2) {
//			if (listeners[i]==FooListener.class) {
//			// Lazily create the event:
//			if (fooEvent == null)
//			fooEvent = new FooEvent(this);
//			((CellEditorListener)listeners[i+1]).fooXXX(fooEvent);
//			}
//		}
//	}
//	@Override
//	public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
//	{
//		PlayerChromosomeComponent result = this;
//		result.setChromosome ((NodeParameters.Player.Chromosome) value);
//		if (isSelected) {
//			this.labelsPanel.setVisible (true);
//			result.setBorder (BorderFactory.createEtchedBorder ());
//		}
//		else {
//			this.labelsPanel.setVisible (hasFocus);
//			result.setBorder (BorderFactory.createEmptyBorder ());
//		}
//		return result;
//	}
//	@Override
//	public Component getTableCellEditorComponent (JTable table, Object value, boolean isSelected, int row, int column)
//	{
//		PlayerChromosomeComponent result = this;
//		result.setChromosome ((NodeParameters.Player.Chromosome) value);
//		if (isSelected) {
//			result.setBorder (BorderFactory.createEtchedBorder ());
//		}
//		else {
//			result.setBorder (BorderFactory.createEmptyBorder ());
//		}
//		return result;
//	}
//
//	@Override
//	public Object getCellEditorValue ()
//	{
//		return this.chromosome;
//	}
//
//	@Override
//	public boolean isCellEditable (EventObject anEvent)
//	{
//		return true;
//	}
//
//	@Override
//	public boolean shouldSelectCell (EventObject anEvent)
//	{
//		return true;
//	}
//
//	@Override
//	public boolean stopCellEditing ()
//	{
//		return false;
//	}
//
//	@Override
//	public void cancelCellEditing ()
//	{
//	}
//
//	@Override
//	public void addCellEditorListener (CellEditorListener l)
//	{
//		this.eventListener.add (CellEditorListener.class, l);
//	}
//
//	@Override
//	public void removeCellEditorListener (CellEditorListener l)
//	{
//		this.eventListener.remove (CellEditorListener.class, l);
//	}
//
//	static private class Editor
//		extends javax.swing.AbstractCellEditor
//		implements javax.swing.table.TableCellEditor
//	{
//
//		@Override
//		public Object getCellEditorValue () {
//			throw new UnsupportedOperationException ("Not supported yet.");
//		}
//
//		@Override
//		public Component getTableCellEditorComponent (JTable table, Object value, boolean isSelected, int row, int column) {
//			throw new UnsupportedOperationException ("Not supported yet.");
//		}
//
//	}
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {
      java.awt.GridBagConstraints gridBagConstraints;

      labelsPanel = new javax.swing.JPanel();
      jLabel1 = new javax.swing.JLabel();
      jLabel2 = new javax.swing.JLabel();
      jLabel3 = new javax.swing.JLabel();
      jLabel4 = new javax.swing.JLabel();
      editorsPanel = new javax.swing.JPanel();
      poolSizeFormattedTextField = new javax.swing.JFormattedTextField();
      utilityThresholdFormattedTextField = new javax.swing.JFormattedTextField();
      deltaFormattedTextField = new javax.swing.JFormattedTextField();
      bitsPerProbabilityFormattedTextField = new javax.swing.JFormattedTextField();

      setBorder(javax.swing.BorderFactory.createTitledBorder("chromosome"));
      setLayout(new java.awt.GridBagLayout());

      labelsPanel.setLayout(new java.awt.GridLayout(1, 4));

      jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      jLabel1.setText("pool size");
      jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
      labelsPanel.add(jLabel1);

      jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      jLabel2.setText("<html>utility<br>threshold</html>");
      jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
      labelsPanel.add(jLabel2);

      jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      jLabel3.setText("delta");
      jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
      labelsPanel.add(jLabel3);

      jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
      jLabel4.setText("<html>bits per<br>probability</html>");
      jLabel4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
      labelsPanel.add(jLabel4);

      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
      add(labelsPanel, gridBagConstraints);

      editorsPanel.setLayout(new java.awt.GridLayout(1, 4));

      poolSizeFormattedTextField.setColumns(5);
      poolSizeFormattedTextField.setValue(new Integer (this.chromosome.getPoolSize ()));
      poolSizeFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            poolSizeFormattedTextFieldPropertyChange(evt);
         }
      });
      editorsPanel.add(poolSizeFormattedTextField);

      utilityThresholdFormattedTextField.setColumns(5);
      utilityThresholdFormattedTextField.setValue(new Float (this.chromosome.getUtilityThreshold ()));
      utilityThresholdFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            utilityThresholdFormattedTextFieldPropertyChange(evt);
         }
      });
      editorsPanel.add(utilityThresholdFormattedTextField);

      deltaFormattedTextField.setColumns(5);
      deltaFormattedTextField.setValue(new Float (this.chromosome.getDelta ()));
      deltaFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            deltaFormattedTextFieldPropertyChange(evt);
         }
      });
      editorsPanel.add(deltaFormattedTextField);

      bitsPerProbabilityFormattedTextField.setColumns(5);
      bitsPerProbabilityFormattedTextField.setValue(new Byte (this.chromosome.getBitsPerProbability ()));
      bitsPerProbabilityFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            bitsPerProbabilityFormattedTextFieldPropertyChange(evt);
         }
      });
      editorsPanel.add(bitsPerProbabilityFormattedTextField);

      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
      add(editorsPanel, gridBagConstraints);
   }// </editor-fold>//GEN-END:initComponents

	 private void bitsPerProbabilityFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_bitsPerProbabilityFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			Byte value = (Byte) this.bitsPerProbabilityFormattedTextField.getValue ();
			try {
				this.chromosome.setBitsPerProbability (value);
				this.firePropertyChange ("chromosome", null, null);
				this.bitsPerProbabilityFormattedTextField.setForeground (Options.ok);
				this.error &= ~1;
			}
			catch (IllegalArgumentException e) {
				this.bitsPerProbabilityFormattedTextField.setForeground (Options.error);
				this.error |= 1;
			}
		}
	 }//GEN-LAST:event_bitsPerProbabilityFormattedTextFieldPropertyChange

	 private void deltaFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_deltaFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			float value = (Float) this.deltaFormattedTextField.getValue ();
			try {
				this.chromosome.setDelta (value);
				this.firePropertyChange ("chromosome", null, null);
				this.deltaFormattedTextField.setForeground (Options.ok);
				this.error &= ~2;
			}
			catch (IllegalArgumentException e) {
				this.deltaFormattedTextField.setForeground (Options.error);
				this.error |= 2;
			}
		}
	 }//GEN-LAST:event_deltaFormattedTextFieldPropertyChange

	 private void utilityThresholdFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_utilityThresholdFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			Float value = (Float) this.utilityThresholdFormattedTextField.getValue ();
			try {
				this.chromosome.setUtilityThreshold (value);
				this.firePropertyChange ("chromosome", null, null);
				this.utilityThresholdFormattedTextField.setForeground (Options.ok);
				this.error &= ~4;
			}
			catch (IllegalArgumentException e) {
				this.utilityThresholdFormattedTextField.setForeground (Options.error);
				this.error |= 4;
			}
		}
	 }//GEN-LAST:event_utilityThresholdFormattedTextFieldPropertyChange

	 private void poolSizeFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_poolSizeFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			Integer value = (Integer) this.poolSizeFormattedTextField.getValue ();
			try {
				this.chromosome.setPoolSize (value);
				this.firePropertyChange ("chromosome", null, null);
				this.poolSizeFormattedTextField.setForeground (Options.ok);
				this.error &= ~8;
			}
			catch (IllegalArgumentException e) {
				this.poolSizeFormattedTextField.setForeground (Options.error);
				this.error |= 8;
			}
		}
	 }//GEN-LAST:event_poolSizeFormattedTextFieldPropertyChange

   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JFormattedTextField bitsPerProbabilityFormattedTextField;
   private javax.swing.JFormattedTextField deltaFormattedTextField;
   private javax.swing.JPanel editorsPanel;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JLabel jLabel3;
   private javax.swing.JLabel jLabel4;
   private javax.swing.JPanel labelsPanel;
   private javax.swing.JFormattedTextField poolSizeFormattedTextField;
   private javax.swing.JFormattedTextField utilityThresholdFormattedTextField;
   // End of variables declaration//GEN-END:variables

}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
